# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=pyelftools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.29
pkgrel=1
pkgdesc='Python library for analyzing ELF files and DWARF debugging information (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/eliben/pyelftools"
license=('custom:Public Domain')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!emptydirs')
source=("https://github.com/eliben/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('591c2b5150f180937c60714a1ae288365b1a3d6ae68aaaa4503dd5ecf5e3c299')

prepare() {
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python test/run_all_unittests.py || warning "Tests failed"
  ${MINGW_PREFIX}/bin/python test/run_examples_test.py || warning "Tests failed"
  if [ "${CARCH}" == "x86_64" ]; then
    ${MINGW_PREFIX}/bin/python test/run_readelf_tests.py || warning "Tests failed"
  else
    echo "Skipping readelf tests (require x86_64)"
  fi
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 README.rst CHANGES -t "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/"
  sed -e '1 { s/^#!.*$// }' -i "${pkgdir}${MINGW_PREFIX}/bin/readelf.py"
  cp -r examples "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/"
}
